<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px">
    <el-form-item label="租户姓名" prop="subscriberName">
      <el-input v-model="dataForm.subscriberName" placeholder="租户姓名" maxlength="100"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号"  maxlength="15"></el-input>
    </el-form-item>
    <el-form-item label="用户号" prop="uname">
      <el-input v-model="dataForm.uname" placeholder="用户号"  maxlength="15"></el-input>
    </el-form-item>    
    <el-form-item label="证件类型" prop="certificateType">
      <el-select v-model="dataForm.certificateType">
        <el-option v-for="item in cartTypedataList" :key="item.paramValue" :label="item.paramValue" :value="item.paramKey" />
      </el-select>
    </el-form-item>
    <el-form-item label="证件号码" prop="certificateValue">
      <el-input v-model="dataForm.certificateValue" placeholder="证件号码" maxlength="30"></el-input>
    </el-form-item>
      <el-form-item label="付费方式" prop="certificateType">
        <!--<el-select v-model="dataForm.subscriberType">-->
          <!--<el-option v-for="item in payTypeList" :key="item.label" :label="item.label" :value="item.key" />-->
        <!--</el-select>-->
        <el-radio-group v-model="dataForm.subscriberType">
          <el-radio :label="0">预付费</el-radio>
          <el-radio :label="1">后付费</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="住址">
        <el-input v-model="dataForm.subscriberAddress" placeholder="住址"  maxlength="200"></el-input>
      </el-form-item>
    <el-form-item label="备注">
      <el-input v-model="dataForm.notes" placeholder="备注"  maxlength="200"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {isMobile} from '@/utils/validate'
  export default {
    data () {
      var validatePhone = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('手机号码不能为空'));
        } else {
          if (!isMobile(value)) {
            callback(new Error('手机号码格式不对'));
          } else {
            callback();
          }
        }
      };
      return {
        cartTypedataList: [],
        visible: false,
        payTypeList:[{"label":"预付费","key":0},{"label":"后付费","key":1}],
        dataForm: {
          subscriberId: 0,
          subscriberName: '',
          subscriberAddress: '',
          phone:'',
          uname: '',
          certificateType: '',
          certificateValue: '',
          notes: '',
          createTime: '',
          subscriberType:0
        },
        dataRule: {
          subscriberName: [
            { required: true, message: '办公室所有者姓名不能为空', trigger: 'blur' }
          ],
          subscriberAddress: [
            { required: true, message: '住址不能为空', trigger: 'blur' }
          ],
          certificateType: [
            { required: true, message: '证件类型', trigger: 'blur' }
          ],
          subscriberType: [
            { required: true, message: '付费方式', trigger: 'blur' }
          ],
          certificateValue: [
            { required: true, message: '证件号码不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号码不能为空', trigger: 'blur' },
            { validator: validatePhone, trigger: 'blur' }
          ],
          notes: [
            { required: true, message: '备注字段不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id,dataList) {
        this.cartTypedataList = dataList ||[]
        this.dataForm.subscriberId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.dataForm.subscriberAddress = ''
          this.dataForm.notes = ''
          if (this.dataForm.subscriberId) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/ecssubscriberinfo/info/${this.dataForm.subscriberId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 0) {
                this.dataForm.subscriberName = data.ecssubscriberinfo.subscriberName
                this.dataForm.subscriberAddress = data.ecssubscriberinfo.subscriberAddress
                this.dataForm.certificateType = data.ecssubscriberinfo.certificateType
                this.dataForm.certificateValue = data.ecssubscriberinfo.certificateValue
                this.dataForm.notes = data.ecssubscriberinfo.notes
                this.dataForm.createTime = data.ecssubscriberinfo.createTime
                this.dataForm.phone = data.ecssubscriberinfo.phone
                this.dataForm.subscriberType = data.ecssubscriberinfo.subscriberType

              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/ecssubscriberinfo/${!this.dataForm.subscriberId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'subscriberId': this.dataForm.subscriberId || undefined,
                'subscriberName': this.dataForm.subscriberName,
                'subscriberAddress': this.dataForm.subscriberAddress,
                'certificateType': this.dataForm.certificateType,
                'certificateValue': this.dataForm.certificateValue,
                'notes': this.dataForm.notes,
                'phone': this.dataForm.phone,
                'uname': this.dataForm.uname,
                'subscriberType': this.dataForm.subscriberType,
                "customerId": this.$util.cookies.get('customer_id'),
              })
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
